package com.topvision.jack.uploadlibrary.admin;


import android.content.Context;

import com.topvision.jack.uploadlibrary.entity.UploadFileEntity;
import com.topvision.jack.uploadlibrary.presenter.IUploadPresenter;
import com.topvision.jack.uploadlibrary.presenter.UploadPresenterImpl;
import com.topvision.jack.uploadlibrary.task.UploadTaskListener;

import java.util.Map;

/**
 * 文件上传SDK
 * Created by gushenjie on 17/12/24.
 */

public class TvUploadSdk {

    //单例模式
    private static TvUploadSdk instance;
    Context context;
    IUploadPresenter uploadPresenter;


    private TvUploadSdk(Context context) {
        this.context = context;
        uploadPresenter = new UploadPresenterImpl(context);
    }

    public static TvUploadSdk getInstance(Context context) {
        if (instance == null) {
            synchronized (TvUploadSdk.class) {
                if (instance == null) {
                    instance = new TvUploadSdk(context);
                }
            }
        }
        return instance;
    }


    /**
     * 上传单个文件
     * @param apiUrl 接口地址
     * @param path 文件地址
     * @param uploadTaskListener 文件回调函数
     */
    public void upload(String apiUrl, String path, UploadTaskListener uploadTaskListener) {
        upload(apiUrl, path, 0, uploadTaskListener);
    }


    /**
     * 上传单个文件
     * @param apiUrl
     * @param path
     * @param blockLength
     * @param uploadTaskListener
     */
    public void upload(String apiUrl, String path, int blockLength, UploadTaskListener uploadTaskListener) {
        UploadFileEntity uploadFileEntity = new UploadFileEntity();
        uploadFileEntity.setFilePath(path);
        uploadFileEntity.setBlockLength(blockLength);
        uploadPresenter.upload(apiUrl, uploadFileEntity, uploadTaskListener);

    }

    /**
     * 上传单个文件
     * @param apiUrl 服务器请求地址
     * @param path 路径
     * @param blockLength 区块大小
     * @param uploadTaskListener 上传事件回调
     * @param data 服务器交互数据
     */
    public void upload(String apiUrl, String path, int blockLength, UploadTaskListener uploadTaskListener, Map<String,String> data) {
        UploadFileEntity uploadFileEntity = new UploadFileEntity();
        uploadFileEntity.setFilePath(path);
        uploadFileEntity.setData(data);
        uploadFileEntity.setBlockLength(blockLength);
        uploadPresenter.upload(apiUrl, uploadFileEntity, uploadTaskListener);

    }



    //上传所有之前未上传成功的任务
    public void uploadTasks(String baseApiUrl, UploadTaskListener uploadTaskListener) {
        uploadPresenter.uploadTasks(baseApiUrl, uploadTaskListener);
    }


    public void pauseTasks() {
        uploadPresenter.pauseTasks();
    }


    /**
     * 注册网络变化监听
     * 创建时间:2017/12/25
     * 作者 jack
     * 描述 有网络则进行上传,无网络则暂停上传
     */
    public void registerNetWorkListener(String baseApi, UploadTaskListener uploadTaskListener) {
        uploadPresenter.registerNetWorkListener(baseApi, uploadTaskListener);
    }


    /**
     * 取消网络监听
     * 创建时间:2017/12/25
     * 作者 jack
     * 描述
     */
    public void unRegisterNetWorkListener() {
        uploadPresenter.unRegisterNetWorkListener();
    }

    public void uploadPhoto(String videoUrl, String photoPath) {
    }
}
